<?php
require_once __DIR__ . '/../vendor/autoload.php';

try {
	(new Dotenv\Dotenv ( __DIR__ . '/../' ))->load ();
} catch ( Dotenv\Exception\InvalidPathException $e ) {
	//
}

$app = new Laravel\Lumen\Application ( realpath ( __DIR__ . '/../' ) );

$app->withFacades ();
$app->withEloquent ();

$app->configure ( 'jwt' );
$app->configure ( 'api' );

class_alias ( 'Tymon\JWTAuth\Facades\JWTAuth', 'JWTAuth' );
class_alias ( 'Tymon\JWTAuth\Facades\JWTFactory', 'JWTFactory' );
class_alias ( 'Dingo\Api\Facade\API', 'API' );
class_alias ( 'Dingo\Api\Facade\Route', 'Route' );
class_alias ( 'Illuminate\Support\Facades\Response', 'Response' );
class_alias ( 'Illuminate\Support\Facades\Config', 'Config' );

$app->singleton ( Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\Handler::class );
$app->singleton ( Illuminate\Contracts\Console\Kernel::class, App\Console\Kernel::class );
$app->singleton ( Illuminate\Cache\CacheManager::class, function ($app) {
	return $app->make ( 'cache' );
} );
$app->singleton ( Illuminate\Auth\AuthManager::class, function ($app) {
	return $app->make ( 'auth' );
} );

$app->middleware ( [ 
		App\Http\Middleware\ExampleMiddleware::class 
] );

$app->routeMiddleware ( [ 
		'auth' => App\Http\Middleware\Authenticate::class,
		'jwt.auth' => Tymon\JWTAuth\Middleware\GetUserFromToken::class,
		'jwt.refresh' => Tymon\JWTAuth\Middleware\RefreshToken::class,
		'cors' => App\Http\Middleware\LumenCors::class,
] );

$app->register ( App\Providers\AppServiceProvider::class );
$app->register ( App\Providers\AuthServiceProvider::class );
$app->register ( App\Providers\EventServiceProvider::class );
$app->register ( Illuminate\Redis\RedisServiceProvider::class );
$app->register ( Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class );
$app->register ( Dingo\Api\Provider\LumenServiceProvider::class );

$app->configureMonologUsing ( function (Monolog\Logger $monoLog) use($app) {
	return $monoLog->pushHandler ( new \Monolog\Handler\RotatingFileHandler ( $app->storagePath () . '/logs/lumen.log', 5 ) );
} );

$app->group ( [ 
		'namespace' => 'App\Http\Controllers' 
], function ($app) {
	require __DIR__ . '/../routes/web.php';
	require __DIR__ . '/../routes/api.php';
} );

return $app;
